//
//  ZBBContentListenNewsRecomendCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBContentListenNewsRecomendCell.h"
#import "TwoToOne.h"
#import "ZBBRecomendCell.h"
#import "ZBBHeadView.h"
#import "HeadView.h"
#import "ZBBScrollViewModel.h"
@implementation ZBBContentListenNewsRecomendCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //[self getData];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];

        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_collectionView];
        [_collectionView registerClass:[ZBBRecomendCell class] forCellWithReuseIdentifier:@"identifiter"];
        [_collectionView registerClass:[ZBBHeadView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [_collectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
        
    
    }
    return self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _dataSourceArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
     else
     {
         
         
         return 3;
     }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake(self.frame.size.width, 90);
    }
    
    else
    {
        return CGSizeMake(self.frame.size.width, 90);

    }
    
    
    
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        
        return CGSizeMake(self.frame.size.width, 200);
    }
    
    else
    {
        return CGSizeMake(self.frame.size.width, 60);
    }
    
    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ZBBHeadView *headView = [collectionView
        dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        
        [headView.scrollview setImageViewsWithArray:_scroviewArray];
        return headView;
        
    }
    else
    {
   HeadView *headView = [collectionView
                                 dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1" forIndexPath:indexPath];
        if (_dataSourceArray.count != 0) {
            ZBBListenRecomendModel *model = _dataSourceArray[indexPath.section];
            headView.leftLabel.text = model.title;
        }
        
        
       
        return headView;

    }
    
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  ZBBRecomendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifiter" forIndexPath:indexPath];
    if (indexPath.section == 0) {
         cell.backgroundColor = [UIColor greenColor];
        cell.flag = 1;
        
    }
    else
    {
       
        ZBBListenRecomendModel *model = _dataSourceArray[indexPath.section];
        
        NSMutableArray *array = model.listArray;
        
        for ( S1mpl3MoreRecommend *model1 in array) {
            
            cell.model = model1;
        }
        
        //cell.backgroundColor = [UIColor redColor];

    }
   
    return cell;
    
    
    
}
-(void)setUrlString:(NSString *)urlString
{
    
    _count++;
    
    if (_count == 2) {
        _scroviewArray = [NSMutableArray array];
        _dataSourceArray = [NSMutableArray arrayWithObject:@""];
        NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%@&contentType=album&device=android&scale=2&version=4.3.14.3", urlString];
        [TwoToOne getDataByString:url BodyString:nil WithDataBlock:^(id data) {
            
            NSDictionary *scroViewDic = [data objectForKey:@"focusImages"];
            NSArray *scroViewArray = [scroViewDic objectForKey:@"list"];
            for (NSDictionary *dic in scroViewArray) {
                
                ZBBScrollViewModel *model = [[ZBBScrollViewModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [_scroviewArray addObject:model];
            }
            
            
            NSDictionary *categoryContents = [data objectForKey:@"categoryContents"];
            NSArray *contentList = [categoryContents objectForKey:@"list"];

            for (NSInteger i = 1; i < contentList.count; i++) {
                NSDictionary *dicContent = contentList[i];
                ZBBListenRecomendModel *model = [[ZBBListenRecomendModel alloc]init];
                [model setValuesForKeysWithDictionary:dicContent];
                NSArray *listArray = [dicContent objectForKey:@"list"];
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *listDic in listArray) {
                    S1mpl3MoreRecommend *model1 = [[S1mpl3MoreRecommend alloc]init];
                    [model1 setValuesForKeysWithDictionary:listDic];
                    [array addObject:model1];
                }
                model.listArray = array;
                [array removeAllObjects];
                [_dataSourceArray addObject:model];
            }
            
            
            
            
            
            [_collectionView reloadData];
            
            
            
            
            
            
            
        }];
        
        

    }
   
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
